package org.mijia.dotsbox.rcp;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchAdvisor;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
import org.mijia.dotsbox.DotsboxApp;
import org.mijia.dotsbox.utils.LogUtils;

public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor {

	private static final String PERSPECTIVE_ID = "org.mijia.dotsbox.perspective";

    public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
        return new ApplicationWorkbenchWindowAdvisor(configurer);
    }

	public String getInitialWindowPerspectiveId() {
		return PERSPECTIVE_ID;
	}
	
	@Override
	public void eventLoopException(Throwable exception) {
		super.eventLoopException(exception);
		exception.printStackTrace();
        LogUtils.error(exception.getMessage());
        Display display = getWorkbenchConfigurer().getWorkbench().getDisplay();
        Shell shell = new Shell(display);
        MessageDialog.openError(shell, "Runtime Error",
                "We've got a serious problem, please check out the log file!\n\n"
                        + exception.getMessage());
	}

	@Override
	public void postShutdown() {
		super.postShutdown();
		DotsboxApp.getInstance().dispose();
	}

	@Override
	public void preStartup() {
		super.preStartup();
		DotsboxApp.getInstance().initInstance();
	}
	
}
